package com.duoketech.controller.web.mfood;

import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.mfood.MemberFood;
import com.duoketech.service.web.mfood.MemberFoodService;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class MemberFoodController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",10);
        Page<MemberFood> result = (Page<MemberFood>) MemberFoodService.ME.pageAllRecord(getModel(MemberFood.class),pageNum,pageSize,Kv.create());
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            setAttr("memberFood",MemberFoodService.ME.queryBy(id,getModel(MemberFood.class),Kv.create()));
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            MemberFoodService.ME.doAdd(getModel(MemberFood.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            MemberFoodService.ME.update(getModel(MemberFood.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        MemberFoodService.ME.deleteBy(id,getModel(MemberFood.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }
  }